package com.jilian.mydemo.modul.exam.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.NestedScrollView;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.common.dto.StudyDto;
import com.jilian.mydemo.common.dto.StudyListDto;
import com.jilian.mydemo.modul.user.viewmodel.UserViewModel;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.NumberUtils;
import com.jilian.mydemo.utils.ToastUitl;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.List;

/**
 * 学习手册
 */
public class LearningManualActivity extends BaseActivity {
    private TextView tvCount;
    private ProgressBar progressBar;
    private TextView tvProgress;
    private ImageView ivFinish;
    private TextView tvContent;
    private TextView tvA;
    private TextView tvB;
    private TextView tvC;
    private TextView tvD;
    private TextView tvOk;
    private UserViewModel viewModel;
    private List<StudyDto> list;
    private int index = 1;
    private NestedScrollView srHasData;
    private SmartRefreshLayout srNoData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }


    @Override
    protected void createViewModel() {
        viewModel = ViewModelProviders.of(this).get(UserViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_learning_manual;
    }

    @Override
    public void initView() {
        setNormalTitle("学习手册", v -> finish());
        tvCount = (TextView) findViewById(R.id.tv_count);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        tvProgress = (TextView) findViewById(R.id.tv_progress);
        ivFinish = (ImageView) findViewById(R.id.iv_finish);
        tvContent = (TextView) findViewById(R.id.tv_content);
        tvA = (TextView) findViewById(R.id.tv_a);
        tvB = (TextView) findViewById(R.id.tv_b);
        tvC = (TextView) findViewById(R.id.tv_c);
        tvD = (TextView) findViewById(R.id.tv_d);
        tvOk = (TextView) findViewById(R.id.tv_ok);
        srHasData = (NestedScrollView) findViewById(R.id.sr_has_data);
        srNoData = (SmartRefreshLayout) findViewById(R.id.sr_no_data);
        srNoData.setEnableLoadMore(false);
    }

    @Override
    public void initData() {
        showLoadingDialog();
        getData();
    }

    private void getData() {

        viewModel.study();
        viewModel.getStudyliveData().observe(this, new Observer<BaseDto<StudyListDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<StudyListDto> dto) {
                srNoData.finishRefresh();
                hideLoadingDialog();
                if (dto.isSuccess()) {
                    if (EmptyUtils.isNotEmpty(dto.getData().getList())) {
                        srHasData.setVisibility(View.VISIBLE);
                        srNoData.setVisibility(View.GONE);
                        list = dto.getData().getList();

                        initStudyView();
                    } else {
                        srHasData.setVisibility(View.GONE);
                        srNoData.setVisibility(View.VISIBLE);
                    }
                } else {
                    ToastUitl.showImageToastTips(dto.getMsg());
                    srHasData.setVisibility(View.GONE);
                    srNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    public void initListener() {
        srNoData.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getData();
            }
        });
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(tvOk.getText().toString().equals("开始答题")){
                    finish();
                    startActivity(new Intent(LearningManualActivity.this, AnswerQuestionsActivity.class));
                }else{
                    initStudyView();
                }



            }
        });

    }

    private void initStudyView() {
        if(index==list.size()){
            ivFinish.setVisibility(View.VISIBLE);
            tvProgress.setVisibility(View.GONE);
            tvOk.setBackgroundResource(R.drawable.shape_btn_login_normal);
            tvOk.setTextColor(getResources().getColor(R.color.white));
            progressBar.setProgress(100);
            tvOk.setText("开始答题");
        }
        tvCount.setText(index + "/" + list.size());
        String progress = NumberUtils.forNormalMatNumber((index / (Double.valueOf(list.size()))) * 100);
        tvProgress.setText(progress + "%");
        progressBar.setMax(list.size());
        progressBar.setProgress(index);
        tvContent.setText(index + "." + list.get(index - 1).getContent());
        tvA.setText("A."+list.get(index - 1).getAnswerA());
        tvB.setText("B."+list.get(index - 1).getAnswerB());
        tvC.setText("C."+list.get(index - 1).getAnswerC());
        tvD.setText("D."+list.get(index - 1).getAnswerD());
        if ("A".equals(list.get(index-1).getAnswer())) {
            tvA.setBackgroundResource(R.drawable.shape_main_dark_100);
            tvA.setTextColor(getResources().getColor(R.color.color_main_title));

            tvB.setBackgroundResource(R.drawable.shape_dark_100);
            tvB.setTextColor(getResources().getColor(R.color.color_text));

            tvC.setBackgroundResource(R.drawable.shape_dark_100);
            tvC.setTextColor(getResources().getColor(R.color.color_text));

            tvD.setBackgroundResource(R.drawable.shape_dark_100);
            tvD.setTextColor(getResources().getColor(R.color.color_text));
        }
        if ("B".equals(list.get(index-1).getAnswer())) {
            tvB.setBackgroundResource(R.drawable.shape_main_dark_100);
            tvB.setTextColor(getResources().getColor(R.color.color_main_title));

            tvA.setBackgroundResource(R.drawable.shape_dark_100);
            tvA.setTextColor(getResources().getColor(R.color.color_text));

            tvC.setBackgroundResource(R.drawable.shape_dark_100);
            tvC.setTextColor(getResources().getColor(R.color.color_text));

            tvD.setBackgroundResource(R.drawable.shape_dark_100);
            tvD.setTextColor(getResources().getColor(R.color.color_text));
        }
        if ("C".equals(list.get(index-1).getAnswer())) {
            tvC.setBackgroundResource(R.drawable.shape_main_dark_100);
            tvC.setTextColor(getResources().getColor(R.color.color_main_title));

            tvA.setBackgroundResource(R.drawable.shape_dark_100);
            tvA.setTextColor(getResources().getColor(R.color.color_text));

            tvB.setBackgroundResource(R.drawable.shape_dark_100);
            tvB.setTextColor(getResources().getColor(R.color.color_text));

            tvD.setBackgroundResource(R.drawable.shape_dark_100);
            tvD.setTextColor(getResources().getColor(R.color.color_text));


        }
        if ("D".equals(list.get(index-1).getAnswer())) {
            tvD.setBackgroundResource(R.drawable.shape_main_dark_100);
            tvD.setTextColor(getResources().getColor(R.color.color_main_title));


            tvA.setBackgroundResource(R.drawable.shape_dark_100);
            tvA.setTextColor(getResources().getColor(R.color.color_text));

            tvB.setBackgroundResource(R.drawable.shape_dark_100);
            tvB.setTextColor(getResources().getColor(R.color.color_text));

            tvC.setBackgroundResource(R.drawable.shape_dark_100);
            tvC.setTextColor(getResources().getColor(R.color.color_text));
        }
        index++;
    }

}
